package spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_execute;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.name_space.executor.task.task_item.Status;

import java.util.UUID;


@Slf4j
@Setter
@Getter
public class TaskItemExecuteRuntimeContext {
    public boolean isReported = false;//是否汇报执行信息
    //
    public volatile boolean needStop = false;//外部指令
    public volatile String needStopMessage;
    //
    protected String nameSpace;
    protected String executorId;
    protected String taskId;
    protected String taskInstanceId;
    protected Integer taskItemIndex;
    protected String taskItemInstanceId;
    //
    private String threadName;
    private Thread.State threadState;
    private Long beginTime;
    private Long completeTime;
    //线程监控信息
    private Long lastTaskMonitorThreadReportTime;
    //任务自身信息
    private Long lastTaskSelfReportTime;
    //任务项续期信息
    private int initRenewalTimeMinutes;
    private Long initRenewalTime;//初始允许续期的时间
    private int maxRenewalTimeMinutes;
    private Long maxRenewalTime;//max允许续期的时间
    private Long renewalInterval = 1000l * 60 * 5;
    private Long MaxAllowRunningTimeInterval = 1000l * 60 * 4;
    //
    private Long lastTaskRenewalSuccessTime;//续期时间(对外不允许其他执行器进行执行任务)
    private Long lastTaskMaxAllowRunningTime;//任务执行最大允许的执行时间(对内如果没有进行续期则程序需要终止)
    private Long nextTaskMaxAllowRunningTime;//下次进行续期的时间
    //执行状态信息
    private int status = Status.RUNNING;
    private String message = "";

    public TaskItemExecuteRuntimeContext(String nameSpace, String executorId, String taskId, String taskInstanceId, Integer taskItemIndex, String taskItemInstanceId, int initRenewalTimeMinutes, int maxRenewalTimeMinutes) {
        this.nameSpace = nameSpace;
        this.executorId = executorId;
        this.taskId = taskId;
        this.taskInstanceId = taskInstanceId;
        this.taskItemIndex = taskItemIndex;
        this.taskItemInstanceId = taskItemInstanceId;
        //
        this.threadState = Thread.State.NEW;
        //
        this.beginTime = System.currentTimeMillis();
        //任务自身信息
        this.lastTaskSelfReportTime = beginTime;
        //线程监控信息
        this.lastTaskMonitorThreadReportTime = beginTime;

        //任务项续期信息
        this.initRenewalTimeMinutes = initRenewalTimeMinutes;
        this.initRenewalTime = beginTime + initRenewalTimeMinutes * 1000l * 60;
        //
        this.maxRenewalTimeMinutes = maxRenewalTimeMinutes;
        this.maxRenewalTime = beginTime + maxRenewalTimeMinutes * 1000l * 60;//最大续期时间要比time out时间要短 这个等价于分布式lock时间
        //
        this.lastTaskRenewalSuccessTime = initRenewalTime;//续期时间
        this.lastTaskMaxAllowRunningTime = initRenewalTime - 1000 * 30;//任务执行最大允许的执行时间

    }

    public final String createThreadName() {
        String threadName = "task-item-execute:" + nameSpace + "/" + executorId + "/" + taskId + "/" + taskInstanceId + "/" + taskItemIndex + "/" + taskItemInstanceId + "/" + UUID.randomUUID();
        return threadName;
    }

    public boolean isFinished() {
        return Status.RUNNING != status;//只要不是运行都表示任务结束了
    }

    public void statusCalculate() {
        if (status != Status.RUNNING) {
            return;
        } else {

        }

    }
}
